package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	//针对WithDeadline的封装。5秒后自动调用cancelFunc
	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
	go Speak2(ctx)

	time.Sleep(10 * time.Second)
}

func Speak2(ctx context.Context) {
	for range time.Tick(time.Second) {
		select {
		//获取上下文通道的数据
		case <-ctx.Done():
			fmt.Println("终止执行")
			return
		default:
			fmt.Println("Speak, 执行中")
		}
	}
}
